上傳檔案 - LoRa SX127x driver for (Micro)Python

需先安裝 ampy ( Adafruit MicroPython Tool )

pip install adafruit-ampy

https://github.com/adafruit/ampy


In [1]:
import os
import shutil

設定COM port (set current COM port), baud rate


In [2]:
baud_rate = 115200
# baud_rate = 230400
# baud_rate = 460800
# baud_rate = 921600

com_port = 'COM3'
com_port = 'COM6'
# com_port = 'COM7'
# com_port = 'COM12'
# com_port = 'COM13'
# com_port = 'COM15'
# com_port = 'COM16'
# com_port = 'COM17'
# com_port = '/dev/ttyUSB0'

Utility functions


In [3]:
def clear_local_folder(folder):
    print('\n[Clearing folder {}]'.format(folder))
    for file in os.listdir(folder):
        os.remove(os.path.join(folder, file))
        
        
def copy_one_file_to_local_folder(folder, file, destination_folder):
    print('Copying {} to {}'.format(file, destination_folder))
    shutil.copy(os.path.join(folder, file), destination_folder)
    
    
def copy_all_files_to_local_folder(folders, destination_folder):
    print('\n[Copying all files to upload folder {}]'.format(destination_folder))
    clear_local_folder(destination_folder)    
    
    for folder in folders: 
        for file in os.listdir(folder):
            if (file.endswith('.py') or file.endswith('.mpy')) and not file.startswith('_'):
                copy_one_file_to_local_folder(folder, file, destination_folder)

In [4]:
def list_files_in_device(com_port):
    files = !ampy --port {com_port} --baud {baud_rate} ls
    return sorted(files)


def cat_file_from_device(com_port, file):
    !ampy --port {com_port} --baud {baud_rate} get {file}
    

def delete_file_in_device(com_port, file):
    print('Deleting {}'.format(file))
    !ampy --port {com_port} --baud {baud_rate} rm {file}
    

def delete_files_in_device(com_port):
    print('\n[Deleting all files in device {}]'.format(com_port))
    for file in list_files_in_device(com_port):
        delete_file_in_device(com_port, file)
        
#     try:
#         !ampy --port {com_port} --baud {baud_rate} rmdir {'/'}
#     except Exception as e:
#         print(e)        
        
        
def copy_one_file_to_device(com_port, folder, file, mpy_only = False):
    if mpy_only:
        if file.endswith('.mpy'):
            print('Copying {}'.format(file))
            !ampy --port {com_port} --baud {baud_rate} put {os.path.join(folder, file)}  
    elif file.endswith('.py'):
        print('Copying {}'.format(file))
        !ampy --port {com_port} --baud {baud_rate} put {os.path.join(folder, file)}   
        

def delet_main_in_device(com_port, main_file_names):
    print('Deleting {}'.format(main_file_names))
    files = list_files_in_device(com_port)

    for file in main_file_names:
        if file in files:
            delete_file_in_device(com_port, file)
            
            
def delete_main_and_files_in_device(com_port, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):
    print('\n[Copying all files to device {}]'.format(com_port))
    delet_main_in_device(com_port, main_file_names)
    if mpy_only: delete_first = True
    if delete_first: delete_files_in_device(com_port)
        

def copy_all_files_to_device(com_port, folder, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):    
    for file in os.listdir(folder):
        if (file.endswith('.py') or file.endswith('.mpy')) and not file.startswith('_') and not file in main_file_names:
            copy_one_file_to_device(com_port, folder, file, mpy_only)                    
    
    for file in main_file_names:
        if os.path.isfile(os.path.join(folder, file)):
            copy_one_file_to_device(com_port, folder, file, mpy_only)  
            
            
def do_all_to_device(com_port, folder, main_file_names = ['main.py', 'main.mpy'], delete_first = True, mpy_only = False):
    delete_main_and_files_in_device(com_port, main_file_names, delete_first, mpy_only)
    copy_all_files_to_device(com_port, folder, main_file_names, delete_first, mpy_only)

Copy *.py to device (all needed fils will be put in the same folder)


In [5]:
folders = [os.path.sep.join(['..', '..', 'codes', 'controller']),
           os.path.sep.join(['..', '..', 'codes', 'demo']),
           os.path.sep.join(['..', '..', 'codes', 'display']),
           os.path.sep.join(['..', '..', 'codes', 'sx127x']),]

upload_folder = os.path.sep.join(['upload', 'py'])

copy_all_files_to_local_folder(folders, upload_folder)


[Copying all files to upload folder upload\py]

[Clearing folder upload\py]
Copying boot.py to upload\py
Copying config_lora.py to upload\py
Copying controller.py to upload\py
Copying controller_esp.py to upload\py
Copying controller_esp_ttgo_lora_oled.py to upload\py
Copying controller_rpi.py to upload\py
Copying main.py to upload\py
Copying LoRaDumpRegisters.py to upload\py
Copying LoRaDuplex.py to upload\py
Copying LoRaDuplexCallback.py to upload\py
Copying LoRaPingPong.py to upload\py
Copying LoRaReceiver.py to upload\py
Copying LoRaReceiverCallback.py to upload\py
Copying LoRaReceiverCallback_dual_channels.py to upload\py
Copying LoRaSender.py to upload\py
Copying LoRaSetSpread.py to upload\py
Copying LoRaSetSyncWord.py to upload\py
Copying test.py to upload\py
Copying test_dual_channels.py to upload\py
Copying display.py to upload\py
Copying display_ssd1306_i2c.py to upload\py
Copying display_ssd1306_spi.py to upload\py
Copying oled_test.py to upload\py
Copying ssd1306.py to upload\py
Copying sx127x.py to upload\py

In [6]:
upload_folder_py = os.path.sep.join(['upload', 'py'])

do_all_to_device(com_port, upload_folder, delete_first = True)

print('\n[All done!]')


[Copying all files to device COM6]
Deleting ['main.py', 'main.mpy']

[Deleting all files in device COM6]
Deleting /boot.py
Copying boot.py
Copying config_lora.py
Copying controller.py
Copying controller_esp.py
Copying controller_esp_ttgo_lora_oled.py
Copying controller_rpi.py
Copying display.py
Copying display_ssd1306_i2c.py
Copying display_ssd1306_spi.py
Copying LoRaDumpRegisters.py
Copying LoRaDuplex.py
Copying LoRaDuplexCallback.py
Copying LoRaPingPong.py
Copying LoRaReceiver.py
Copying LoRaReceiverCallback.py
Copying LoRaReceiverCallback_dual_channels.py
Copying LoRaSender.py
Copying LoRaSetSpread.py
Copying LoRaSetSyncWord.py
Copying oled_test.py
Copying ssd1306.py
Copying sx127x.py
Copying test.py
Copying test_dual_channels.py
Copying main.py

[All done!]

Copy *.mpy to device


In [ ]:
upload_folder_py = os.path.sep.join(['upload', 'py'])
upload_folder_mpy = os.path.sep.join(['upload', 'mpy'])

do_all_to_device(com_port, upload_folder_mpy, mpy_only = True)

copy_one_file_to_device(com_port, upload_folder_py, 'boot.py')
copy_one_file_to_device(com_port, upload_folder_py, 'main.py')

print('\n[All done!]')

單一檔案上傳 (single file upload, in case needed)


In [38]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller.py')


Copying controller.py

In [39]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller_esp.py')


Copying controller_esp.py

In [13]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'controller_esp_ttgo_lora_oled.py')


Copying controller_esp_ttgo_lora_oled.py

In [39]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'main.py')


Copying main.py

In [8]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'controller']), 'config_lora.py')


Copying config_lora.py

In [7]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'sx127x']), 'sx127x.py')


Copying sx127x.py

In [42]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'test.py')


Copying test.py

In [28]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'test_dual_channels.py')


Copying test_dual_channels.py

In [6]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSender.py')


Copying LoRaSender.py

In [165]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaReceiver.py')


Copying LoRaReceiver.py

In [187]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSetSpread.py')


Copying LoRaSetSpread.py

In [44]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaSetSyncWord.py')


Copying LoRaSetSyncWord.py

In [94]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaReceiverCallback.py')


Copying LoRaReceiverCallback.py

In [109]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaDuplex.py')


Copying LoRaDuplex.py

In [35]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaDuplexCallback.py')


Copying LoRaDuplexCallback.py

In [33]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'demo']), 'LoRaPingPong.py')


Copying LoRaPingPong.py

In [14]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'display']), 'ssd1306.py')


Copying ssd1306.py

In [10]:
copy_one_file_to_device(com_port, os.path.sep.join(['..', '..', 'codes', 'display']), 'display_ssd1306_i2c.py')


Copying display_ssd1306_i2c.py

列出檔案 (list files)


In [ ]:
# list_files_in_device(com_port)

檢查檔案內容 (check file content)


In [9]:
# cat_file_from_device(com_port, 'config_lora.py')

連網測試 (network config and test)


In [ ]:
# 連上網路
# import network; nic=network.WLAN(network.STA_IF); nic.active(False);  # disable network
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('SSID','password');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.active(True); nic.connect('Kingnet-70M-$370', '');nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF); nic.ifconfig()
# import network; nic=network.WLAN(network.STA_IF);nic.ifconfig();nic.config('mac');nic.ifconfig((['mac',])

In [ ]:
# Delete all files
# import u_python;u_python.del_all_files();import os;os.listdir()